// pages/my/my.js

const {
    imgConfig,
    config
} = require('../../utils/config.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: `${imgConfig}`,
        statusBarHeight: app.globalData.statusBarHeight,
        status: wx.getStorageSync("status"),
        loginStatus: false,
        loginBox: true
    },
    //获取用户信息的回调
    getUserProfile(e) {
        wx.getUserProfile({
            desc: '资料',
            success: (res) => {
                //修改userInfo的状态数据
                this.setData({
                    userInfo: res.userInfo,
                })
                app.globalData.userInfo = res.userInfo
                app.globalData.userInfo.photo = res.userInfo.avatarUrl
                app.globalData.userInfo.stuName =  res.userInfo.nickName//姓名
            },
            fail: (err) => {
            }
        })

    },
    //获取手机号
    getPhoneNumber(e) {
        //点击的“拒绝”或者“允许
        var that = this
        wx.login({
            success: res => {
                wx.request({ //获取授权手机号
                    url: `${config}/wechat/wxlogin/codingphonenum`,
                    data: {
                        'encryptedData': e.detail.encryptedData,
                        'iv': e.detail.iv,
                        'code': e.detail.code
                    },
                    method: 'GET',
                    header: {
                        'content-type': 'application/json'
                    },
                    success: function (res) {
                        //授权成功后将后台查询的学生信息放入Storage中
                        wx.setStorageSync('phoneNumber', res.data.response.body.phone_info.purePhoneNumber);
                        that.setData({
                            phoneNumber:res.data.response.body.phone_info.purePhoneNumber
                        })
                        if (res.data.response.body.errcode == 0) {
                            //发送请求查询学生表中是否存在  若 存在则保存openId和头像信息便于下次登录直接访问 不存在给出提示
                            wx.request({
                                url: `${config}/wechat/wxlogin/saveOpenId`,
                                data: {
                                    // openId: that.data.openid,
                                    phoneNumber: that.data.phoneNumber,
                                    avatarUrl: that.data.userInfo.avatarUrl,
                                    deptId: app.globalData.deptId,
                                    nickname: that.data.userInfo.nickName
                                },   
                                //登录成功跳转至首页
                                success(resp) {
                                 console.log("=============================================")
                                 console.log( resp.data)
                                 console.log(app.globalData);
                                    if (resp.data.errMsg == "学员登录成功") {
                                       app.globalData.userInfo = resp.data.studentInfo
                                        that.getLastClock()
                                        that.setData({
                                            loginStatus: !that.data.loginStatus,
                                            loginBox: !that.data.loginBox,
                                            avatar: app.globalData.userInfo.photo, //头像
                                            stuName: app.globalData.userInfo.stuName, //姓名
                                            phoneNumber: app.globalData.userInfo.stuTel, //手机号
                                        })
                                    } else if (resp.data.errMsg == "访客首次登录成功") {
                                     //   app.globalData.userInfo = resp.data.visitorInfo
                                        app.globalData.visitorStatus = true
                                        console.log(app.globalData.stuidByscene);
                                        if (app.globalData.stuidByscene != 0) {
                                          wx.request({
                                            url: `${config}/erp/erpWeChat/erpwechatintegralrecord/addIntegralNewPersion`,
                                            method: 'POST',
                                            data: {
                                                stuId: app.globalData.stuidByscene,
                                                stuTel:app.globalData.shareStuTel,
                                                touristTel: resp.data.visitorInfo.stuTel,
                                                touristId: resp.data.visitorInfo.id,
                                                touristDeptId:app.globalData.shareStuDeptid,
                                            },
                                        })
                                        }
                                        that.setData({
                                            loginStatus: !that.data.loginStatus,
                                            loginBox: !that.data.loginBox,
                                            avatar: app.globalData.userInfo.photo, //头像
                                            stuName: app.globalData.userInfo.nickName, //姓名
                                            phoneNumber: app.globalData.userInfo.stuTel, //手机号
                                        })
                                    } else if (resp.data.errMsg == "访客登录成功") {
                                      console.log(resp.data);
                                      console.log(app.globalData.stuidByscene);
                                      console.log(app.globalData);
                                      if (app.globalData.stuidByscene != 0) {
                                        wx.request({
                                          url: `${config}/erp/erpWeChat/erpwechatintegralrecord/addIntegralNewPersion`,
                                          method: 'POST',
                                          data: {
                                              stuId: app.globalData.stuidByscene,
                                              stuTel:app.globalData.shareStuTel,
                                              touristTel: resp.data.visitorInfo.stuTel,
                                              touristId: resp.data.visitorInfo.id,
                                              touristDeptId:app.globalData.shareStuDeptid,
                                          },
                                      })
                                      }
                                        app.globalData.visitorStatus = true
                                      //  app.globalData.userInfo = resp.data.visitorInfo
                                        that.setData({
                                            loginStatus: !that.data.loginStatus,
                                            loginBox: !that.data.loginBox,
                                            avatar: app.globalData.userInfo.photo, //头像
                                            stuName: app.globalData.userInfo.nickName, //姓名
                                            phoneNumber: app.globalData.userInfo.stuTel, //手机号
                                        })
                                    }else { //没有权限  关闭小程序
                                        wx.showModal({
                                          showCancel: false,
                                          title: "提示",
                                          content: '没有登录权限,请联系管理人员',
                                          complete(res) {
                                            wx.switchTab({
                                              url: '/pages/index/index',
                                            })
                                          }
                                        })
                                      }
                                }
                            })
                        }
                    },
                    fail: function (err) {
                    }
                })
            }
        })
    },
    /**
     * 获取最新打卡参数
     */
    getLastClock() {
        var that = this
        var studentInfo = app.globalData.userInfo
        //发送获取课程信息的请求
        wx.request({
            url: `${config}/wechat/wxclock/getCourseChapterList`,
            data: {
                majorId: studentInfo.majorId
            },
            success(resp) {
                //课程信息赋值给前台
                app.globalData.courseChapterList = resp.data.courseChapterList
            },
        })
        //globalData中存入新的打卡数据 用于返回后访问打卡页使用
        console.log(app.globalData.userInfo);
        wx.request({
            url: `${config}/wechat/wxclock/getLastClock`,
            data: {
                stuId: app.globalData.userInfo.id
            },
            success(res) {
                if (res.data.clockEntity == null || res.data.clockEntity == undefined) {
                    return
                }
                if (res.data.clockEntity.updateTime == null) {
                    var clockStatus = true
                } else {
                    var clockStatus = false
                }
                app.globalData.clockStatus = clockStatus //添加打卡状态 页面显示 上课/下课打卡
                app.globalData.courseId = res.data.clockEntity.courseId
                app.globalData.chapterId = res.data.clockEntity.chapterId
                var courseId = res.data.clockEntity.courseId
                var chapterId = res.data.clockEntity.chapterId
                var courseChapterList = app.globalData.courseChapterList
                for (var i = 0; i < courseChapterList.length; i++) {
                    if (courseChapterList[i].id == courseId) {
                        var courseIndex = i //添加课程列表索引
                    }
                }
                app.globalData.courseIndex = courseIndex
                var chapterList = courseChapterList[courseIndex].chapter
                for (var i = 0; i < chapterList.length; i++) {
                    if (chapterList[i].id == chapterId) {
                        var chapterIndex = i //添加章节列表索引
                    }
                }
                app.globalData.chapterIndex = chapterIndex

            }
        })
    },
    goMessage() {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: '../message/message',
            })
        }
    },
    goScratchList() {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: "../scratchlist/scratchlist"
            })
        }
    },
    goContract() {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: "../contract/contract"
            })
        }
    },
    goIntegral() {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: '/pages/integral/integral?shop='+0,
            })
        }
    },
    goExamscore() {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: '/pages/examscore/examscore',
            })
        }
    },

    goHomework() {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: '/pages/homework/homework',
            })
        }
    },
    goAttendance() {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: '../attendance/attendance',
            })
        }
    },
    // 我的请假
    goLeavelist: function () {
      console.log(app.globalData);
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: "../leavelist/leavelist"
            })
        }
    },

    // 退出 demo
    goOut: function () {
        var that = this
        wx.showModal({
            title: '提示',
            content: '确认要退出登录吗？',
            success: function (res) {
                if (res.confirm) {
                    wx.showToast({
                        title: '您已退出登录',
                        duration: 2000
                    })
                    setTimeout(function () {
                        that.setData({
                            loginStatus: false,
                            loginBox: true
                        })
                        wx.switchTab({
                            url: '/pages/index/index',
                        })
                    }, 2000)

                }
            }
        })
    },
    goConsume: function () {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: "/pages/consume/consume"
            })
        }
    },
    goCourse: function () {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: "/pages/course/course"
            })
        }
    },
    goClock: function () {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: "../clock/clock"
            })
        }
    },
    goClockOff: function () {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: "../clockoff/clockoff"
            })
        }
    },
    goLeave: function () {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: "../leave/leave"
            })
        }
    },
    goFileUpload: function () {
        if (app.globalData.visitorStatus) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '提示',
                content: '报名后可使用此功能',
                showCancel: false
            })
        } else {
            wx.navigateTo({
                url: "../homework/homework"
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 适配IphoneX
        let isIphoneX = app.globalData.isIphoneX;
        this.setData({
            isIphoneX: isIphoneX
        });

    },

    goShop(){
      if (app.globalData.visitorStatus) {
          wx.showModal({
              cancelColor: 'cancelColor',
              title: '提示',
              content: '报名后可使用此功能',
              showCancel: false
          })
      } else {
          wx.navigateTo({
              url: '/pages/integral/integral?shop='+1,
          })
      }
    }
})